package com.wardrobe.soa.common;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.processing.OperationManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

/**
 * Created by m1304 on 2018/8/8.
 */
public class QiniuFileUploadClient {

    private static final Logger logger = LoggerFactory.getLogger(QiniuFileUploadClient.class.getSimpleName());

    private static SimpleDateFormat df= new SimpleDateFormat("yyMMdd/HHmm");

    private static int taskDelay = 1800000;
    private static int taskPeriod = 1800000;

    private Auth auth;
    private UploadManager uploadManager;
    private OperationManager operationManager;
    private String token;
    private String bucketName;
    private Timer timer;
    private Configuration cfg;

    class TokenTask extends TimerTask {

        @Override
        public void run() {
            token = auth.uploadToken(bucketName);
        }

    }
    public QiniuFileUploadClient(String accessKey, String secretKey, String bucketName) {
        //构造一个带指定Zone对象的配置类
        cfg = new Configuration(Zone.zone1());
        auth = Auth.create(accessKey, secretKey);

        uploadManager = new UploadManager(cfg);
        operationManager = new OperationManager(auth,cfg);
        this.bucketName = bucketName;
        token = auth.uploadToken(bucketName);
        timer = new Timer();
        timer.schedule(new TokenTask(), taskDelay, taskPeriod);
    }

    public boolean fileUploadByToken(byte[] data, String key,String token) throws QiniuException {
        Response response = uploadManager.put(data, key, token);
        if (response.isOK()) {
            return true;
        } else {
            throw new QiniuException(response);
        }
    }

    public String fileUpload(byte[] data, String business, String fileType, String ext) throws QiniuException {
        String fileName = getFileName(business, fileType, ext);
        Response response = uploadManager.put(data, fileName, token);
        if (response.isOK()) {
            return fileName;
        } else {
            throw new QiniuException(response);
        }
    }

    public String fileUpload(String name,byte[] data, String business, String fileType, String ext) throws QiniuException {
        String fileName = getFileNameForStaticFile(name,business, fileType, ext);
        Response response = uploadManager.put(data, fileName, token);
        if (response.isOK()) {
            return fileName;
        } else {
            throw new QiniuException(response);
        }
    }

    public String getToken(String bucketName, String key, StringMap policy) {
        return auth.uploadToken(bucketName, key, 3600L, policy);
    }

    public String getFileName(String business, String fileType, String ext) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(fileType).append("/").append(business).append("/").append(df.format(new Date())).append("/")
                .append(UUID.randomUUID().toString().replace("-", "").substring(0, 16)).append(".").append(ext);
        return buffer.toString();
    }

    public String getFileNameForStaticFile(String name,String business, String fileType, String ext) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(fileType).append("/").append(business).append("/").append(df.format(new Date())).append("/")
                .append(name).append(".").append(ext);
        return buffer.toString();
    }

    public String uploadById(byte[] data, String business, String fileType, String ext, String id) throws QiniuException {
        StringBuffer buffer = new StringBuffer();
        buffer.append(fileType).append("/").append(business).append("/").append(id).append(".").append(ext);
        String fileName = buffer.toString();
        Response response = uploadManager.put(data, fileName, token);
        if (response.isOK()) {
            return fileName;
        } else {
            throw new QiniuException(response);
        }
    }

    public String pfop(String bucket, String key, String fops, StringMap params) throws QiniuException {
        return operationManager.pfop(bucket, key, fops, params);
    }

}
